import FWCore.ParameterSet.Config as cms

##Suport for reading files
import FWCore.Utilities.FileUtils as FileUtils

"""

How to obtain a list?

#only last 10 files to test
cat 2B2C1Jet_job_config | awk  '{print $1}' | tail -10 >| job_config_cmsRun_genParticles

#or do 

./SrmFiles2Dcap "srm://dcache-se-cms.desy.de:8443/srm/managerv2?SFN=/pnfs/desy.de/cms/tier2/store/user/marfin/marfin/Alpgen2b3j_2b4j" "2b3j" "root" >| job
_config_cmsRun_genParticles_2b3j
 

"""

mylist = FileUtils.loadListFromFile ('job_config_cmsRun_genParticles') 
readFiles = cms.untracked.vstring( *mylist)



process = cms.Process("TEST2")


## add message logger
process.load("FWCore.MessageLogger.MessageLogger_cfi")
process.MessageLogger.cerr.threshold = 'INFO'



## define input

process.source = cms.Source('PoolSource', fileNames = readFiles )
#process.source.firstRun        = cms.untracked.uint32(34)
#process.source.firstEvent      = cms.untracked.uint32(67)


"""

###local input (for testing )

process.source = cms.Source("PoolSource",
   fileNames = cms.untracked.vstring(
"dcap://dcache-cms-dcap.desy.de:22125//pnfs/desy.de/cms/tier2/store/mc/Summer11/2B2C1Jet_TuneZ2_7TeV-alpgen-pythia6/AODSIM/PU_S4_START42_V11-v1/0000/F847F864-78FD-E011-8DCD-0030487CDA4C.root"



     ),
     skipEvents = cms.untracked.uint32(0)
)
"""

##I belive it might help with generation different lumi numbers

RandomNumberGeneratorService = cms.Service(
     "RandomNumberGeneratorService",
     # This is to initialize the random engine of the source
     generator = cms.PSet(
         initialSeed = cms.untracked.uint32(123456789),
         engineName = cms.untracked.string('TRandom3')
     ),

     # This is to initialize the random engines used for  Famos
     VtxSmeared = cms.PSet(
         initialSeed = cms.untracked.uint32(123456789),
         engineName = cms.untracked.string('TRandom3')
     ),
	
	 sourceSeed = cms.PSet(
	initialSeed =	cms.untracked.uint32(123456789),
	   engineName = cms.untracked.string('TRandom3')
)


)

## define maximal number of events to loop over
process.maxEvents = cms.untracked.PSet(    
input = cms.untracked.int32(-1) 
)

## configure process options
process.options = cms.untracked.PSet(
    wantSummary = cms.untracked.bool(True)
)

## configure geometry & conditions
process.load("Configuration.StandardSequences.Geometry_cff")
process.load("Configuration.StandardSequences.MagneticField_cff")
process.load("Configuration.StandardSequences.FrontierConditions_GlobalTag_cff")



## Summer11 MC
process.GlobalTag.globaltag = cms.string('START42_V12::All')



## std sequence for pat
#process.load("PhysicsTools.PatAlgos.patSequences_cff")



# We are working with GenLevel PATLayer1 must be empty
process.patDefaultSequence = cms.Sequence()


####All other lines might be skipped if you don't want to do some work


###Lets create reco::GenJets

###needed by genParticles
process.load("SimGeneral.HepPDTESSource.pythiapdt_cfi")


#1) genParticles
process.genParticles = cms.EDProducer("GenParticleProducer",
    saveBarCodes = cms.untracked.bool(True),
    src = cms.InputTag("generator"),
    abortOnUnknownPDGCode = cms.untracked.bool(False)
 )


process.patDefaultSequence+=process.genParticles
#process.patDefaultSequence2+=process.genParticles


#2) 	genParticlesForJets
process.load("RecoJets.Configuration.GenJetParticles_cff")

process.patDefaultSequence+=process.genParticlesForJets
#process.patDefaultSequence2+=process.genParticlesForJets

#3) ak5GenJets

process.load("RecoJets.JetProducers.ak5GenJets_cfi")
process.patDefaultSequence+=process.ak5GenJets
#process.patDefaultSequence2+=process.ak5GenJets


###GenMet

#4) GenMETParticles
process.load("RecoMET.Configuration.GenMETParticles_cff")
process.patDefaultSequence+=process.genParticlesForMETAllVisible
#process.patDefaultSequence2+=process.genParticlesForMETAllVisible


#5) genMetTrue

process.load("RecoMET.METProducers.genMetTrue_cfi")
process.patDefaultSequence+=process.genMetTrue
#process.patDefaultSequence2+=process.genMetTrue





process.TFileService=cms.Service("TFileService",fileName=cms.string('samples.root'))


###Matching

process.patJetPartonMatch = cms.EDProducer("MCMatcher",        # cut on deltaR, deltaPt/Pt; pick best by deltaR
    src         = cms.InputTag("ak5GenJets"),       # RECO objects to match
     matched     = cms.InputTag("genParticles"),      # mc-truth particle collection
     mcPdgId     = cms.vint32(1, 2, 3, 4, 5, 21),     # one or more PDG ID (quarks except top; gluons)
     mcStatus    = cms.vint32(3),                     # PYTHIA status code (3 = hard scattering)
     checkCharge = cms.bool(False),                   # False = any value of the charge of MC and RECO is ok
     maxDeltaR   = cms.double(0.4),                   # Minimum deltaR for the match
     maxDPtRel   = cms.double(3.0),                   # Minimum deltaPt/Pt for the match
     resolveAmbiguities    = cms.bool(True),          # Forbid two RECO objects to match to the same GEN object
     resolveByMatchQuality = cms.bool(False),         # False = just match input in order; True = pick lowest deltaR pair first
 )

process.patDefaultSequence+=process.patJetPartonMatch

###BTagging

process.Btagging  = cms.EDFilter("WeightsBtagMakerWrapper",
        src=cms.InputTag("ak5GenJets"),
        matched=cms.InputTag("patJetPartonMatch"),
        cut=cms.string("abs(eta)<5.0 && pt>1")

)


process.patDefaultSequence+=process.Btagging


###MVA Producer
process.MVAProducer = cms.EDProducer('MVAProducerWrapper',
jets=cms.InputTag("ak5GenJets"),
mvaPath = cms.FileInPath("AlpgenAnalysis/AlpgenAnalysis2/data/weights/MVACOMPUTERS"),
#mvaMethod =  cms.vstring("Likelihood", "LikelihoodD","LikelihoodKDE","LikelihoodPCA","LikelihoodMIX")
mvaMethod =  cms.vstring("Likelihood"),
cut = cms.string("abs(eta)<5.0 && pt>1")
)

process.patDefaultSequence+=process.MVAProducer




process.SelectionStep0=cms.EDFilter("SelectionProducerWrapper",
        src=cms.InputTag("ak5GenJets"),
        name=cms.string("step0"),
        type = cms.string("_nocut_"), #
        min=cms.double(-1e+10),
        max=cms.double(0),
        cut=cms.string("abs(eta)<5.0 && pt>1"),
        path=cms.FileInPath("AlpgenAnalysis/AlpgenAnalysis2/data/tree.xml"),
        mvaComp=cms.InputTag("MVAProducer")

)

process.patDefaultSequence+=process.SelectionStep0


process.SelectionStep1=cms.EDFilter("SelectionProducerWrapper",
        src=cms.InputTag("ak5GenJets"),
        name=cms.string("step1"),
        type = cms.string("_ptJet0_"), #
        min=cms.double(48),
        max=cms.double(10000),
#       path=cms.FileInPath("AlpgenAnalysis/AlpgenAnalysis2/data/tree.xml"),
        cut=cms.string("abs(eta)<5.0 && pt>1 ")

)

process.patDefaultSequence+=process.SelectionStep1

process.SelectionStep2=cms.EDFilter("SelectionProducerWrapper",
        src=cms.InputTag("ak5GenJets"),
        name=cms.string("step2"),
        type = cms.string("_ptJet1_"), #
        min=cms.double(36),
        max=cms.double(10000),
#       path=cms.FileInPath("AlpgenAnalysis/AlpgenAnalysis2/data/tree.xml"),
        cut=cms.string("abs(eta)<5.0 && pt>1 ")


)

process.patDefaultSequence+=process.SelectionStep2

process.SelectionStep3=cms.EDFilter("SelectionProducerWrapper",
        src=cms.InputTag("ak5GenJets"),
        name=cms.string("step3"),
        type = cms.string("_ptJet2_"), #
        min=cms.double(15),
        max=cms.double(10000),
#       path=cms.FileInPath("AlpgenAnalysis/AlpgenAnalysis2/data/tree.xml"),
        cut=cms.string("abs(eta)<5.0 && pt>1")


)

process.patDefaultSequence+=process.SelectionStep3


process.SelectionStep4=cms.EDFilter("SelectionProducerWrapper",
        src=cms.InputTag("ak5GenJets"),
        name=cms.string("step4"),
        type = cms.string("_dr01_"), #
        min=cms.double(1.0),
        max=cms.double(10000),
        cut=cms.string("abs(eta)<5.0 && pt>1 "),
#       path=cms.FileInPath("AlpgenAnalysis/AlpgenAnalysis2/data/tree.xml"),
        mvaComp=cms.InputTag("MVAProducer")


)


process.patDefaultSequence+=process.SelectionStep4


process.Tree=cms.EDFilter("TreeProducerWrapper",
        src=cms.InputTag("ak5GenJets"),
        mets=cms.InputTag("genMetTrue"),
        path=cms.FileInPath("AlpgenAnalysis/AlpgenAnalysis2/data/tree.xml"),
        cut=cms.string("abs(eta)<5.0 && pt>1 ")
)


process.patDefaultSequence+=process.Tree

process.SelectionStep5=cms.EDFilter("SelectionProducerWrapper",
        src=cms.InputTag("ak5GenJets"),
        name=cms.string("step5"),
        type = cms.string("_mva0_"), #
        min=cms.double(0),
        max=cms.double(0.26),
        cut=cms.string("abs(eta)<5.0 && pt>1 "),
#        path=cms.FileInPath("AlpgenAnalysis/AlpgenAnalysis2/data/tree.xml"),
        mvaComp=cms.InputTag("MVAProducer")
)

process.patDefaultSequence+=process.SelectionStep5




process.SelectionStep6=cms.EDFilter("SelectionProducerWrapper",
        src=cms.InputTag("ak5GenJets"),
        name=cms.string("step6"),
        type = cms.string("_mva0_"), #
        min=cms.double(0),
        max=cms.double(0.026),
        cut=cms.string("abs(eta)<5.0 && pt>1 "),
#        path=cms.FileInPath("AlpgenAnalysis/AlpgenAnalysis2/data/tree.xml"),
        mvaComp=cms.InputTag("MVAProducer")
)

process.patDefaultSequence+=process.SelectionStep6

process.Sample=cms.EDFilter("SampleProducerWrapper",
        name=cms.string("SAMPLE"),
        type = cms.string("BKG"), #
        weight=cms.double(1),
        xsect=cms.double(1),
        lumi=cms.double(1),
        id=cms.int32(0)
)


process.patDefaultSequence+=process.Sample




process.p = cms.Path(process.patDefaultSequence 
			)


#produce ntuples for publishing

# Output module configuration
#from PhysicsTools.PatAlgos.patEventContent_cff import *
### output module

#process.out = cms.OutputModule( "PoolOutputModule",
#  SelectEvents = cms.untracked.PSet( SelectEvents = cms.vstring('p2')),
## outputCommands = cms.untracked.vstring('drop *'),
# outputCommands = cms.untracked.vstring('keep *'),
##  verbose  = cms.untracked.bool(True),
#  fileName = cms.untracked.string('ntuple.root')
#)

## output path
#process.outpath = cms.EndPath(process.out)


